package frame.system.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;

import frame.base.BaseService;
import frame.base.BootstrapTable;
import frame.system.mapper.ConfParamMapper;
import frame.system.qo.ConfQo;
import frame.system.to.ConfParam;
import tk.mybatis.mapper.common.Mapper;

import javax.annotation.PostConstruct;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;


/**
 * ConfParam 模块服务类
 * <br>
 * @author PeiWentao
 * @since 2020-10-01 23:12
 */
@Service
public class ConfParamService extends BaseService<String, ConfParam, ConfQo> {
	@Autowired
    private ConfParamMapper confParamMapper;

    @Override
    protected Mapper<ConfParam> mainMapper() {
        return confParamMapper;
    }

    // 缓存所有的开关值
    private static final Map<String, ConfParam> caches = new ConcurrentHashMap<>();

    @PostConstruct
    public void cache() {
        caches.clear();
        ConfQo qo = new ConfQo();
        List<ConfParam> list = confParamMapper.pageList(qo);
        list.forEach(param -> {caches.put(param.getId(), param);});
    }


    @Override
	public BootstrapTable<ConfParam> pageList(final ConfQo qo) {
        //分页查询
        Page<ConfParam> page = PageHelper.startPage(qo.page(), qo.size()).doSelectPage(() -> {
            // TODO 分页查询动态条件需要实现
            confParamMapper.pageList(qo);
        });
        return new BootstrapTable<ConfParam>(page.getTotal(),page.getResult());
	}

    @Override
    protected ConfParam newInstanceT(String id) {
        return new ConfParam(id);
    }


    @Override
    public ConfParam addSelective(ConfParam record) {
        super.addSelective(record);
        caches.put(record.getId(), record);
        return record;
    }
    @Override
    public int updateSelective(ConfParam record) {
        int re = super.updateSelective(record);
        caches.put(record.getId(), record);
        return re;
    }

    @Override
    public int delete(String id) {
        caches.remove(id);
        return super.delete(id);
    }

    @Override
    public int remove(String id) {
        caches.remove(id);
        return super.remove(id);
    }
    public static ConfParam getCache(String id) {
        return caches.get(id);
    }

    /**
     * 获取开关
     * @param id
     * @return intVal == 1 =>开 其他情况都为关闭
     */
    public static boolean isOn(String id) {
        ConfParam param = caches.get(id);
        if (param == null) {
            return false;
        } else {
            return Integer.valueOf(1).equals(param.getIntVal());
        }
    }
    public static Integer getIntVal(String id) {
        ConfParam param = caches.get(id);
        if (param == null) {
            return null;
        } else {
            return param.getIntVal();
        }
    }
    public static Double getDouVal(String id) {
        ConfParam param = caches.get(id);
        if (param == null) {
            return null;
        } else {
            return param.getDouVal();
        }
    }
    public static String getStrVal(String id) {
        ConfParam param = caches.get(id);
        if (param == null) {
            return null;
        } else {
            return param.getStrVal();
        }
    }
}
